<?php

namespace App\Http\Controllers\Api;

use App\Lib\Uploads\UploadHandler;
use App\Models\WxAttachment;
use Illuminate\Http\Request;

class ApiController extends BaseController
{
    public function cos_review_callback(Request $request){
        $code = $request->code;
        $message = $request->message;
        if($code === 0){
            $data = $request->data;

            $attachment_id = _array_key($data, 'data_id', 0);
            $attachment = null;
            if($attachment_id > 0){
                $attachment = WxAttachment::find($attachment_id);
            }
            $event = _array_key($data, 'event', '');
            if($event == 'ReviewVideo'){
                $result = _array_key($data, 'result', -1);
                $forbidden_status = _array_key($data, 'forbidden_status', -1);

                if($attachment){
                    if($result == 1){
                        // 判定为违规敏感文件
                        UploadHandler::del($attachment, true, 1);
                    }else if($result == 2){
                        // 冻结
                        if($forbidden_status == 1 || $forbidden_status == 2){
                            $attachment->state = $forbidden_status == 1 ? 3 : 4;
                            $attachment->cos_review = 2;
                            $attachment->save();
                        }else{
                            UploadHandler::frozen($attachment);
                        }
                    }else{
                        if($forbidden_status == 1 || $forbidden_status == 2){
                            $attachment->state = $forbidden_status == 1 ? 3 : 4;
                            $attachment->cos_review = 2;
                            $attachment->save();
                        }else{
                            $attachment->cos_review = 0;
                            $attachment->save();
                        }
                    }
                }
            }
        }else{
            _logger_(__file__, __line__, 'cos回调结果：'.$code.':'.$message);
        }
    }
}
